package com.m3958.firstgwt.client;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;
import com.m3958.firstgwt.client.constants.FirstGwtConstants;
import com.m3958.firstgwt.client.layout.DefaultTopLayout;
import com.m3958.firstgwt.client.resource.FirstGwtResource;
import com.m3958.firstgwt.client.service.AppUiService;
import com.m3958.firstgwt.client.utils.AsyncTaskQuery;
import com.m3958.gwtutil.clientside.client.LoginOutService;
import com.m3958.gwtutil.clientside.client.SharedMenuBar;
import com.m3958.gwtutil.clientside.client.event.IintrestLoginStatus;
import com.m3958.gwtutil.clientside.client.event.LoginEventListeners;
import com.m3958.gwtutil.clientside.client.jso.UserJso;
import com.m3958.gwtutil.clientside.client.utils.CommonAppUtil;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.layout.VLayout;

public class AppShell extends VLayout implements IintrestLoginStatus{
	
	@Inject
	private AppStatus aservice;
	
	@Inject
	private DataPanelManager dpm;
	
	@Inject
	private AsyncTaskQuery atq;
	
	@Inject
	private LoginOutService lios;
	
	@Inject
	private FirstGwtConstants fconstants;
	
	@Inject
	private AppUiService auiservice;
	
	@Inject
	private CommonAppUtil cau;
	
	@Inject
	public AppShell(DefaultTopLayout top,/*DefaultMainMenuLayout mmenu,*/DataPanel dp,SharedMenuBar smb,FirstGwtResource fgr,LoginEventListeners lel){
		setWidth100();
		setHeight100();
		setOverflow(Overflow.AUTO);
		addMember(top);
		VLayout vl = new VLayout();
		vl.setWidth100();
		vl.setHeight100();
		
		VerticalPanel vp = new VerticalPanel();
		vp.setHeight("30px");
		vp.setWidth("100%");
		vp.add(smb);
		
		vl.addMember(vp);
		vl.addMember(dp);
		
		addMember(vl);
		lel.addListener(this);
	}
	
	public void start(){
		aservice.initTimeZone();
		aservice.setDpm(dpm);
		lios.detectLoginStatus();
		String ua = cau.getUserAgent();
//		if("360se".equals(ua)){
//			auiservice.showTmpPrompt("系统对360等壳浏览器的支持没有经过测试，请选择纯正的IE浏览器或者经过良好测试的：谷歌浏览器、火狐浏览器", 5000);
//		}else 
		if("ie6".equals(ua)){
			auiservice.showTmpPrompt(fconstants.ie6warn(), 5000);
		}
//		final AppShell aps = this;
//		Element gotop = DOM.getElementById("topcontrol");
//		Label l = Label.wrap(gotop);
//		l.addClickHandler(new ClickHandler() {
//			@Override
//			public void onClick(ClickEvent event) {
//				aps.scrollToTop();
//				aps.redraw();
//				aps.adjustForContent(true);
//			}
//		});
	}
	
	@Override
	public void loginStatusChanged(boolean isLogined, UserJso user) {
		if(isLogined){
			if(user.isFcuser() && user.getBindTo() < 1){
				Location.replace("/bindexist.html#next=sitebuilder");
				return;
			}
			aservice.start();
			History.addValueChangeHandler(dpm);
			dpm.fillLayouts();
			dpm.handleCurrentHistory();
			atq.startWork();
		}
	}

}
